package org.example.student_system.controller;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.example.student_system.aop.anno;
import org.example.student_system.entity.Teacher;
import org.example.student_system.mapper.TeacherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@RestController
@Tag(name = "登录")
public class LoginController {
    @Autowired
    private TeacherMapper teacherMapper;

    @anno
    @PostMapping("/login")
    @Operation(summary = "登录")
    public String login(String username, String password) {
        log.info("用户{}登录", username);
        Teacher login = teacherMapper.Login(username, password);
        if(login != null) {
            return Jwts.builder()
                    .setSubject(username)
                    .setExpiration(new Date(new Date().getTime() + 1000 * 60 * 60 * 24))
                    .signWith(SignatureAlgorithm.HS256, "liangweijun")
                    .compact();
        }
        else return null;
    }
}
